package com.idea.id_generate.advice;

import com.idea.id_generate.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.task.TaskRejectedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import com.idea.id_generate.rest.ResponseCode;
import com.idea.id_generate.rest.ResponseEntityUtil;
import com.idea.id_generate.rest.ResponseWrapper;

import javax.servlet.http.HttpServletRequest;


/**
 * 系统异常统一处理
 *
 * @author @ 2018年4月19日 下午3:19:19
 */
@Slf4j
@ControllerAdvice(annotations = {RestController.class, Service.class})
public class BaseExceptionHandlerAdvice {


    /**
     * 自定义LiveException处理,自定义业务code、httpStatus状态码(默认500)
     *
     * @param request
     * @param ex
     * @return 异常请求结果
     * 2018年4月20日 上午10:53:08
     */
    @ExceptionHandler({ApiException.class})
    public ResponseEntity<ResponseWrapper<?>> handleApiException(HttpServletRequest request, ApiException ex) {
        log.info(ex.toString());
        ex.printStackTrace();
        // 拦截
        return ResponseEntityUtil.exception(ex.getCode(), ex.getMessage(), ex.getHttpStatus(), request);
    }

    /**
     * NullPointerException处理
     *
     * @param request
     * @param ex
     * @return 异常请求结果
     * 2018年4月20日 上午10:49:32
     */
    @ExceptionHandler({NullPointerException.class})
    public ResponseEntity<ResponseWrapper<?>> handleNullPointerException(HttpServletRequest request, NullPointerException ex) {
        ex.printStackTrace();
        return ResponseEntityUtil.exception(ResponseCode.FAILED_NULL_POINTER_EXCEPTION, ex, HttpStatus.INTERNAL_SERVER_ERROR, request);
    }

    /**
     * IllegalArgumentException处理
     *
     * @param request
     * @param ex
     * @return 异常请求结果
     * 2018年4月20日 上午10:51:38
     */
    @ExceptionHandler({IllegalArgumentException.class})
    public ResponseEntity<ResponseWrapper<?>> handleIllegalArgumentException(HttpServletRequest request, IllegalArgumentException ex) {
        ex.printStackTrace();
        return ResponseEntityUtil.exception(ResponseCode.FAILED_ILLEGAL_ARGUMENT_EXCEPTION, ex, HttpStatus.INTERNAL_SERVER_ERROR, request);
    }


    /**
     * TaskRejectedException处理
     *
     * @param request
     * @param ex
     * @return
     */
    @ExceptionHandler({TaskRejectedException.class})
    public ResponseEntity<ResponseWrapper<?>> handleTaskRejectedException(HttpServletRequest request, TaskRejectedException ex) {
        ex.printStackTrace();
        return ResponseEntityUtil.exception(ResponseCode.FAILED_TASK_REJECTED_EXCEPTION, ex, HttpStatus.INTERNAL_SERVER_ERROR, request);
    }


    /**
     * 通用Exception处理
     *
     * @param request
     * @param ex
     * @return 异常请求结果
     * 2018年4月20日 上午10:52:00
     */
    @ExceptionHandler({Exception.class})
    public ResponseEntity<ResponseWrapper<?>> handleException(HttpServletRequest request, Exception ex) {
        ex.printStackTrace();
        return ResponseEntityUtil.exception(ResponseCode.FAILED, ex, HttpStatus.INTERNAL_SERVER_ERROR, request);
    }
}

